package com.qf.service.impl;

import com.qf.domain.Cart;
import com.qf.domain.Goods;
import com.qf.domain.User;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.util.StringUtil;
import com.qf.web.controller.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/cartservlet")
public class CartServlet extends BaseServlet {//继承BaseServlet
    //添加购物车
    public String addCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");//设置编码
        //判断是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "/redirct:/index.jsp";
        }
        //获取前端数据
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        //校验
        if (StringUtil.isEmpty(goodsId)) {
            request.setAttribute("msg", "商品id不能为空");
            return "/message.jsp";
        }
        //第一次添加给一个默认数量
        int num = 1;//默认数量
        if (!StringUtil.isEmpty(number)) {//如果为空给默认值1
            try {
                num = Integer.parseInt(number);
                if (num < 1 || num > 10) {
                    num = 1;
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        try{
        //创建业务对象
        int goods_id = Integer.parseInt(goodsId);
        //查询商品信息
        GoodsService goodsService = new GoodsServiceImpl();
        Goods goods = goodsService.findById(goods_id);
        CartService cartService = new CartserviceImpl();
        //购物车的对象
//        Cart cart = new Cart(user.getId(), goods_id, num, goods.getPrice().multiply(new BigDecimal(num)));
//        cartService.add(cart);
        //查询购物车是否存在这个商品
          Cart cart =  cartService.find(user.getId(),goods_id);
        //如果存在数量+1
        if(cart!=null){
            cart.setNum(cart.getNum()+num);
            cart.setMoney(goods.getPrice().multiply(new BigDecimal(cart.getNum())));
            cartService.modify(cart);
        }  else {//没有就创建一个
            cart = new Cart(user.getId(), goods_id, num, goods.getPrice().multiply(new BigDecimal(num)));
            cartService.add(cart);
        }
        return "redirect:/cartSuccess.jsp";
    }catch(Exception e){
        e.printStackTrace();
        request.setAttribute("msg","添加购物车失败");
        return "/message.jsp";
    }
    }
    //查看购物车
    public String getCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "/redirct:/login.jsp";
        }
        //创建业务对象
        CartService cartService = new CartserviceImpl();
        try {
            List<Cart>cartList = cartService.find(user.getId());
            //把购物车放入狱中
            request.setAttribute("cartList",cartList);
            //转发
            return "/cart.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","查看购物车失败");
            //转发
            return "/message.jsp";
        }
    }
    //Ajax操作购物车
    public String addCartAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //判断是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "/redirct:/login.jsp";
        }
        //获取前端数据
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        //校验
        if(StringUtil.isEmpty(goodsId)){
            request.setAttribute("msg","商品id不能为空");
            return  "/message.jsp";
        }
        if(StringUtil.isEmpty(number)){
            request.setAttribute("msg","商品id不能为空");
            return  "/message.jsp";
        }
        //执行业务
        try {
            int num = Integer.parseInt(number);
            Integer goods_id = Integer.parseInt(goodsId);
            CartService cartService = new CartserviceImpl();
            if(num==0){//删除商品
                cartService.remove(user.getId(),goods_id);
            }else if(num==1 || num==-1){
                Cart cart = cartService.find(user.getId(), goods_id);
                if (cart != null) {
                    GoodsService goodsService = new GoodsServiceImpl();
                    Goods goods = goodsService.findById(goods_id);
                    cart.setNum(cart.getNum()+num);
                    cart.setMoney(goods.getPrice().multiply(new BigDecimal(cart.getNum())));
                    cartService.modify(cart);//
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","更新购物车失败");
            return  "/message.jsp";
        }
        return null;
    }
    //清空购物车
    public String clearCartAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //判断是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "/redirct:/login.jsp";
        }
       //创建业务对象
        CartService cartService =new CartserviceImpl();
        try {
            cartService.clear(user.getId());
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","更新购物车失败");
            return  "/message.jsp";
        }
        return null;
    }

}
